<?php
error_reporting(E_ERROR);
session_start();
require_once('constants_shop.php');
require_once('functions.php');
include('classes/userClass.php');


#conect to shop database    
$shopObj = new mysqlClass();

$available_lang= $shopObj->fetch_result("SELECT * FROM languages WHERE is_default=1 LIMIT 1");
$active_lang = $available_lang[0]['id'];
//get dictionary
$dictionary = get_dictionary_shop($active_lang);

if(isset($_POST["action"])){

    $produse=array();
    $index=0;
    $result= $shopObj->fetch_result("SELECT DISTINCT stocks.product_id FROM stocks ORDER BY stocks.product_id");
    foreach ($result as $key => $value) {
        $produs=array();
        $produs[0]=intval($value[0]);
        $produse[$index]=$produs; 
        $index++;
    }


    for($i=0;$i<count($produse);$i++){
        $result= $shopObj->fetch_result("SELECT SUM(quantity) FROM stocks WHERE product_id=?",$produse[$i]);

        foreach ($result as $key => $value) {
           
            $produse[$i][1]=intval($value[0]);

        }

    }

        

    $produseAlerta=array();
    $index=0;
    for($i=0;$i<count($produse);$i++){
        if(intval($produse[$i][1])<5){
            $produseAlerta[$index]=$produse[$i];
            $index++;
        }
    }
   
    // echo $active_lang;
    $active_language=2;

    for($i=0;$i<count($produseAlerta);$i++){
        $params=array($produseAlerta[$i][0],$active_language);    
        $result= $shopObj->fetch_result("SELECT name from products_details WHERE  product_id=? AND language_id=?",$params);

        foreach ($result as $key => $value) {
            $produseAlerta[$i][2]=$value[0];
        }
    }

     
    

    // var_dump($result);

    echo json_encode($produseAlerta);


}
    

?>